/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clases;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
 *
 * @author Ele
 */
public class abmCliente {
   
    public void Conectar(){
        try{
            
            mdbc = new conexion();
            mdbc.init();
            Connection conn= mdbc.getMyConnection();
            stmt = conn.createStatement();
        }catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Problemas al concetarse a la Base de Datos","Error",JOptionPane.ERROR_MESSAGE);
        }
    }   
    

 public void AltaCliente(String CUIT,String razonsocial, String domicilio, String telefono)   {
      String comandoAlta= "Insert into cliente (CUIT, razonsocial, domicilio,telefono) values ('"+CUIT+"','"+razonsocial+"','"+domicilio+"','"+telefono+"')";
      
      try{
          Conectar();
          
            int done= stmt.executeUpdate(comandoAlta);
            JOptionPane.showMessageDialog(null,"Se agregó el cliente a la base de datos","Infromación",JOptionPane.INFORMATION_MESSAGE);
            mdbc.close(stmt);
          
        }catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null,ex.getMessage(),"Error en alta cliente",JOptionPane.ERROR_MESSAGE);
        }
      mdbc.destroy();
      } 
    
 public boolean ExisteCliente(String CUIT){
        int b= 0;
        try{
            Conectar();
            ResultSet rs = stmt.executeQuery("select CUIT from Cliente where CUIT  = '"+CUIT+"'");
            System.out.println(rs.first());
            if(rs.first()){

            b=1;

            }
        }catch(Exception ex){

                ex.printStackTrace();
        }
        if(b==1)return true;
        else return false;
    }
 
 
 
 
 public void BajaCliente (String CUIT){
          String comandoBaja= "Delete  from cliente where CUIT='"+CUIT+"'";
      try{
          Conectar();
              int op= JOptionPane.showConfirmDialog(null,"¿Está seguro de eliminar el Cliente con CUIT:"+CUIT,"Confirmación",JOptionPane.YES_NO_OPTION);
              if (op==JOptionPane.YES_OPTION){
              int done = stmt.executeUpdate(comandoBaja);
              JOptionPane.showMessageDialog(null,"Se eliminó el Cliente","ERROR",JOptionPane.ERROR_MESSAGE);
              }
              
                  
            
        }catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null,"Error al dar de baja cliente","ERROR",JOptionPane.ERROR_MESSAGE);
        }
      mdbc.close(stmt);
    mdbc.destroy();
  }   
 
   public ResultSet BuscarCliente (String nombre){
    String comandobuscar = " Select razonsocial as 'Cliente', CUIT, Domicilio, Telefono from cliente where razonsocial LIKE '%"+nombre+"%'";
     ResultSet rs = null;
      try{
          Conectar();
        rs = stmt.executeQuery(comandobuscar);
       
      }
      catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null,"No se encontraron datos cliente","Error",JOptionPane.ERROR_MESSAGE);
        }
       return rs;
  }
    
    
    public String [] BuscaClienteCUIT (String CUIT){
    String comandobuscar = " Select razonsocial as 'Cliente',Domicilio, Telefono from cliente where CUIT='"+CUIT+"'";
     ResultSet rs = null;
     String datos[]= new String [3];
      try{
          Conectar();
          rs = stmt.executeQuery(comandobuscar);
          rs.first();
          datos[0]= rs.getString("Cliente");
          datos[1]= rs.getString("Domicilio");
          datos[2]= rs.getString("Telefono");
         
      }
      catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null,"No se encontraron datos cliente","Error",JOptionPane.ERROR_MESSAGE);
        }
       return datos;
  }  
    
    public void ModificaCliente(String CUITv, String CUITn, String razonsocial, String domicilio, String telefono){
      
      String ComandoModifica= "Update cliente set cuit ='"+CUITn+"', razonsocial='"+razonsocial+"',"
              + "domicilio = '"+domicilio+"', telefono = '"+telefono+"'  where CUIT='"+CUITv+"'";
                try{
                    Conectar();
                      int done = stmt.executeUpdate(ComandoModifica);
                      JOptionPane.showMessageDialog(null,"Se modificaron los datos del cliente","Aviso",JOptionPane.INFORMATION_MESSAGE);
        }catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null,"Error al modificar datos cliente \n"+ex.getMessage() ,"Error",JOptionPane.ERROR_MESSAGE);
        }
                
      mdbc.close(stmt);
    mdbc.destroy();
  
  }
    
    
  private conexion mdbc;
  private Statement stmt;
}
